#coding=utf-8
'''
Created on Aug 3, 2016

@author: wangpeng
'''

#coding=utf-8
'''
Created on Aug 2, 2016

@author: wangpeng
'''
import unittest
from appium import webdriver
from selenium.webdriver.common.by import By   #用于匹配某个值是否出现，智能等待
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import common_setting.logging_save
import common_setting.desired_setting
import common_setting.config
import common_setting.Create_Log_Folder
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import os
icon_xpath = common_setting.config.huawei_BT_setting_icon_open()

desired_setting = common_setting.desired_setting.desired_setting()
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_setting)
time.sleep(5)
driver.start_activity("com.android.settings", ".HWSettings")
common_setting.logging_save.logging.info("app测试开始，开始检测蓝牙名字是否对应")
print time.ctime(),'app测试开始，开始检测蓝牙名字是否对应'
time.sleep(5)
driver.find_element_by_xpath("//android.widget.TextView[@text='蓝牙']").click()
time.sleep(5)
before = str("//android.widget.TextView[@text= '")                                                                                                                                                                                                                
match = common_setting.config.bluetooth_id()
after = str("'] ")
name_detect = before+match+after
print name_detect
#driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_setting)
try:
    WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH,name_detect))) #确认是否进入到主界面                       
    common_setting.logging_save.logging.info("经过确认，可以找到此广播ID值")
    print time.ctime(),"经过确认，可以找到此广播ID值"
    time.sleep(3)
    
except:
    driver.get_screenshot_as_file(save_pic)
    fail("未找到此广播ID值，测试停止")

#判断当前蓝牙是处于连接状态，还是断开状态
try:        
    WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH,"//android.widget.TextView[@text='已连接']"))) #确认是否进入到主界面                       
    common_setting.logging_save.logging.info("经过确认，蓝牙已经处于连接状态")
    print time.ctime(),"经过确认，已经连接成功"
    time.sleep(3)
    driver.find_element_by_xpath(name_detect).click()
    time.sleep(4)
    driver.find_element_by_xpath("//android.widget.Button[@text='确定']").click()
    common_setting.logging_save.logging.info("断开蓝牙操作")
    print time.ctime(),"断开蓝牙操作"
    time.sleep(10)
    
except:
    pass
   
####让BT speaker进入配对状态，等待youjunpen状态反馈，放蓝牙配对命令
##########################################等待   


common_setting.logging_save.logging.info("蓝牙开始配对")
print time.ctime(),"蓝牙开始配对"
driver.find_element_by_xpath(name_detect).click()    #连接设备
time.sleep(10)
#el = driver.find_element_by_xpath("//android.widget.TextView[@text='已连接']")
#driver.find_element_by_xpath("driver.find//android.widget.TextView[@text='已连接']")
'''
try:
    #检测是否正确，如果有错误就报错
    assertIsNotNone(driver.find_element_by_xpath("//android.widget.TextView[@text='已连接']"))
    common_setting.logging_save.logging.info("正常检测到蓝牙连接成功")
    print time.ctime(),"正常检测到蓝牙连接成功"
except:
    driver.get_screenshot_as_file(save_pic)
    fail("未检测到蓝牙处于连接状态，请确认问题所在")
'''